package com.alm.handle.mq.Impl;

import com.alm.handle.mq.IDevCommandService;
import com.alm.handle.mq.ISysCommandService;
import com.alm.handle.mq.SysService;
import com.alm.handle.redis.RedisService;
import com.alm.handle.util.ByteUtils;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DevCommandServiceImpl implements IDevCommandService {

    @Autowired
    private SysService sysService;

    @Autowired
    private RedisService redisService;

    private static final Logger logger = LoggerFactory.getLogger(DevCommandServiceImpl.class);

    @Override
    public boolean sendDataByByte(String deviceId, byte[] data) {
        System.out.println(ByteUtils.bytesToString(data));
        ISysCommandService sysCommandService = sysService.getSysCommandService();
        //调用业务
        deviceId = deviceId.toUpperCase();
        String ctxId = redisService.getCtxIdByDeviceId(deviceId);
        if (ctxId == null) {
            logger.info("B send failed, not found ctx  by byte");
            return false;
        }
        try {
            boolean result = sysCommandService.sendDataBytype(ctxId, data);
            if (result) {
                logger.info("send message to remote success by byte " + ByteUtils.getStringByByteArr(data));
                return true;
            }
            logger.info("send message to remote fail  by byte");
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("send message to remote exception by byte");
            return false;
        }
    }
}
